package com.duolebo.uteped_sdk.remind;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.service.notification.StatusBarNotification;
import android.text.SpannableString;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.duolebo.uteped_sdk.utils.Const;
import com.duolebo.uteped_sdk.utils.DeviceHelper;
import com.duolebo.uteped_sdk.utils.Logger;
import com.yc.nadalsdk.bean.MessageInfo;
import com.yc.nadalsdk.bean.Notify;
import com.yc.nadalsdk.ble.open.UteBleDevice;
import com.yc.nadalsdk.constants.RemindType;

/**
 * @Author: LiuYang
 * @Date: 2023/12/27 22:03
 * @Description:
 */
public class NotificationRemind extends BaseRemind {

    private final static String TAG = "NotificationRemind";

    @SuppressLint("StaticFieldLeak")
    private static NotificationRemind instance;
    public static NotificationRemind getInstance(Context context) {
        if (instance == null) {
            instance = new NotificationRemind(context);
        }

        return instance;
    }

    public static NotificationRemind shareInstance() {
        return instance;
    }

    public NotificationRemind(Context context) {
        super(context);
    }

    public void onNotificationPosted(StatusBarNotification sbn) {
        Bundle extras = sbn.getNotification().extras;
        Object objTitle = extras.getCharSequence(Notification.EXTRA_TITLE);
        Object objText = extras.getCharSequence(Notification.EXTRA_TEXT);
        String title = "";// Unknown message title 默认通知标题
        String text = "";// Unknown message content 默认通知内容
        if (null != objTitle) {
            if (objTitle instanceof SpannableString) {
                title = ((SpannableString) objTitle).subSequence(0, ((SpannableString) objTitle).length()).toString();
                Logger.d(TAG, "title: " + ((SpannableString) objTitle).subSequence(0, ((SpannableString) objTitle).length()).toString());
            } else {
                title = objTitle.toString();
                Logger.d(TAG, "title: " + objTitle);
            }
        }

        if (null != objText) {
            if (objText instanceof SpannableString) {
                text = ((SpannableString) objText).subSequence(0, ((SpannableString) objText).length()).toString();
                Logger.d(TAG, "text: " + ((SpannableString) objText).subSequence(0, ((SpannableString) objText).length()).toString());
            } else {
                text = objText.toString();
                Logger.d(TAG, "text: " + objText);
            }
        }

        boolean sendNotify = false;
        String packageName = sbn.getPackageName();
        Logger.d(TAG, "package name:" + packageName);
        if (!TextUtils.isEmpty(packageName) && !isExcludePackage(packageName)) {
            sendNotify = DeviceHelper.getInstance().read(packageName, false);

            // 第三方apk通知开启
            if (!sendNotify
                    && !Const.REMIND_PACKAGES.contains(packageName)
                    && !CameraRemind.shareInstance().isCameraOpen()) {
                sendNotify = DeviceHelper.getInstance().read(Const.APP_REMIND_OTHER_PACKAGE, false);
            }
        }

        if (sendNotify) {
            String appName = "";
            try {
                PackageManager packageManager = context.getPackageManager();
                appName = packageManager.getApplicationInfo(packageName, 0).loadLabel(packageManager).toString();
            } catch (PackageManager.NameNotFoundException e) {
                throw new RuntimeException(e);
            }
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setTitle(title);
            messageInfo.setContent(text);
            messageInfo.setAppName(appName);
            messageInfo.setType(getMessageType(packageName));
            messageInfo.setSourcePackage(packageName);
            messageInfo.setVibrate(true);

            sendMessageInfo(messageInfo);
        }
    }

    private boolean isExcludePackage(String packageName) {
        if ("com.android.mms|com.android.sms|com.android.messaging".contains(packageName)) {
            return true;
        }

        return false;
    }

    private int getMessageType(String packageName) {
        int type;
        switch (packageName) {
            case "com.tencent.tim":
            case "com.tencent.mobileqq":
                type = RemindType.TYPE_QQ;
                break;
            case "com.tencent.mm":
                type = RemindType.TYPE_WECHAT;
                break;
            case "com.instagram.android":
                type = RemindType.TYPE_INSTAGRAM;
                break;
            case "com.google.android.youtube":
                type = RemindType.TYPE_YOUTUBE;
                break;
            case "com.facebook.katana":
                type = RemindType.TYPE_FACEBOOK;
                break;
            case "com.twitter.android":
                type = RemindType.TYPE_TWITTER;
                break;
            case "com.whatsapp":
                type = RemindType.TYPE_WHATSAPP;
                break;
            case "com.skype.raider":
                type = RemindType.TYPE_SKYPE;
                break;
            case "com.snapchat.android":
                type = RemindType.TYPE_SNAPCHAT;
                break;
            case "jp.naver.line.android":
                type = RemindType.TYPE_LINE;
                break;
            case "com.facebook.orca":
                type = RemindType.TYPE_FACEBOOK_MESSENGER;
                break;
            case "com.linkedin.android":
                type = RemindType.TYPE_LINKEDIN;
                break;
            case "com.viber.voip":
                type = RemindType.TYPE_VIBER;
                break;
            case "com.kakao.talk":
                type = RemindType.TYPE_KAKAO_TALK;
                break;
            case "com.vkontakte.android":
                type = RemindType.TYPE_VKONTAKTE;
                break;
            case "com.google.android.gm":
                type = RemindType.TYPE_GMAIL;
                break;
            case "com.flickr.android":
                type = RemindType.TYPE_FLICKR;
                break;
            case "com.tumblr":
                type = RemindType.TYPE_TUMBLR;
                break;

            default:
                type = RemindType.TYPE_OTHERS;
        }

        return type;
    }

    @Override
    public void onNotify(@NonNull UteBleDevice uteBleDevice, @NonNull Notify notify) {

    }
}
